package com.rs.yyds.service.impl;

import com.mysql.cj.util.StringUtils;
import com.rs.yyds.bean.MenuVo;
import com.rs.yyds.bean.User;
import com.rs.yyds.mapper.MenuMapper;
import com.rs.yyds.mapper.UserMapper;
import com.rs.yyds.service.UserService;
import com.rs.yyds.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 根据用户名密码查询用户以及菜单角色信息
     * @param user
     * @return
     */
    @Override
    public Map<String,Object> findUserByEmailAndPwd(User user) {
        Map<String,Object> map = new HashMap<String,Object>();
        if(StringUtils.isNullOrEmpty(user.getUserEmail())
                || StringUtils.isNullOrEmpty(user.getUserPassword())){
            map.put("code",1000);
            map.put("msg","用户名或密码错误");
            return map;
        }
//        将输入的密码MD5加密之后再存入user中去
        user.setUserPassword(MD5Util.getMD5Code(user.getUserPassword()));

        User us = userMapper.findUserByEmailAndPwd(user);
        if(us==null){
            map.put("code",1001);
            map.put("1001","用户不存在");
            return map;
        }
        List<MenuVo> menuVoList1 = new ArrayList<MenuVo>();
//        查询一级菜单的条件
        MenuVo menuVo = new MenuVo();
        menuVo.setRoleId(us.getUserRole());
        menuVo.setPid(0);
//        查询一级菜单
        List<MenuVo> menuVoList=menuMapper.findRoleMenuList(menuVo);
        for(MenuVo mv : menuVoList) {
//            赋值二级菜单的查询条件，将一级菜单的主键id赋值给二级菜单的pid
            menuVo.setPid(mv.getId());
//            查询二级菜单
            List<MenuVo> menuVoList2 = menuMapper.findRoleMenuList(menuVo);
            mv.setMenuVo(menuVoList2);
            menuVoList1.add(mv);
        }
        map.put("code",0);
        map.put("msg","success");
        map.put("User",us);
        map.put("MenuList",menuVoList1);
        return map;
    }

    @Override
    public int updateUserById(User user) {
        return userMapper.updateUserId(user);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> addUser(User user) {
        Map<String, Object> map = new HashMap<>();
//        根据邮箱查询用户是否已经存在
        User us = new User();
        us.setUserEmail(user.getUserEmail());
        User uss=userMapper.findUserByEmailAndPwd(us);
        if(uss!=null){
            map.put("code",1);
            map.put("msg","用户已存在");
            return map;
        }
        user.setUserPassword(MD5Util.getMD5Code(user.getUserPassword()));
        int i = userMapper.insertUser(user);
        if(i<=0){
            map.put("code",2);
            map.put("msg","新增用户失败");
            return map;
        }
        map.put("code",0);
        map.put("msg","success");
        return map;
    }

    @Override
    public List<User> findUser(User user) {
        return userMapper.findAllByPage(user);
    }

    @Override
    public int deleteUsers(int[] ids) {
        return userMapper.updateBatch(ids);
    }
}
